#include "EpollServerCplus.h"
#include "PthreadPoolCplus.h"

struct epoll_event events[MAX_EVENTS]; //返回epoll一次性触发的数量

using namespace std;

void* func(void* arg)
{
	myevent_s *ev = (myevent_s *) arg;
	printf("ev->fd;%d,ev->events:%d,ev->status:%d,ev->s_offset:%d\n",ev->fd,ev->events,ev->status,ev->s_offset);
	ev->last_active = time(NULL);
}

int main()
{
	CPthreadPool cPthreadPool;
	cPthreadPool.pool_create();

	EpollEvents epollserver;
	epollserver.InitListenSocket(9999);
	int listenfd =0;
	int fds 	 =0;
	myevent_s *ev;
	
	listenfd = epollserver.GetListenID();
	
	while(true)
	{
		
		epollserver.CheckTimeOut();
		fds =epollserver.EpollWait(events);//events 在EpollServerCplus.h 中已声明
		for(int i = 0; i< fds; i++)
		{
			ev = (struct myevent_s*)events[i].data.ptr;
			//此处Accept所对应的listenfd描述符,一直处于打开状态,且活动状态,所以会一直调用AcceptConn函数
			printf("events[i].events:%d\n,ev->events%d,ev->fd:%d\n",events[i].events,ev->events,ev->fd);
			sleep(3);
			if((events[i].events&EPOLLIN)&&(ev->events&EPOLLIN))
			{	
				if(listenfd == ev->fd)
				{
					epollserver.AcceptConn(listenfd, 0, NULL);
				}
				else
				{
					//向线程池内增加工作任务
					cPthreadPool.pool_add_work(&func,ev);
				}
			}
		
		}
	}
	return 0;
}
